﻿namespace EyeOpen.BomberMan
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	public class MapCreator
	{
		/// <exception cref="InvalidOperationException">All lines in the maps must have the same length.</exception>
		public Map Create(IEnumerable<string> lines, Action mapChanged)
		{
			var y = 0;
			var x = 0;

			var firstLine = lines.First();

			var cellsY = lines.Count();
			var cellsX = firstLine.Count();

			var map = new ItemType[cellsX, cellsY];

			foreach (var line in lines)
			{
				if (line.Count() != cellsX)
				{
					throw new InvalidOperationException("All lines in the maps must have the same length.");
				}

				foreach (var c in line)
				{
					map[x, y] = (ItemType)byte.Parse(c.ToString());
					x++;
				}

				x = 0;
				y++;
			}

			return
				new Map(map, mapChanged);
		}
	}
}